<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

class omecsv_ctl_admin_import extends omecsv_prototype{

    function main(){
        $render = kernel::single('desktop_controller');
        $render->pagedata['ctler'] = $_GET['ctler'];
        $render->pagedata['add'] = $_GET['add'];
        $render->pagedata['_finder'] = $_GET['_finder']['finder_id'];
        $render->pagedata['finder_id'] = $_GET['finder_id'];
		
        $get = kernel::single('base_component_request')->get_get();
        try {
            $oName = substr($get['ctler'],strlen($get['add'].'_mdl_'));
            $model = app::get($get['add'])->model( $oName );
        } catch (Exception $e) {
            $msg = $e->getMessage();
            echo $msg;exit;
        }
        unset($get['app'],$get['ctl'],$get['act'],$get['add'],$get['ctler']);
        $render->pagedata['data'] = $get;
        
        if (method_exists($model, 'import_input')) {
            $render->pagedata['import_input'] = $model->import_input();
        }

        $render->display('common/import.html','omecsv');
    }


    /**
    * 进度条导入上传
    */
    public function progress_import()
    {
        $get = kernel::single('base_component_request')->get_get();
        $render = kernel::single('desktop_controller');
        $importApp = $get['import_app'];
        $importCtl = $get['import_ctl'];
        $importAct = $get['import_act'];
        $importMdl = $get['import_mdl'];
        $importFilter = $get['import_filter'];
        $importTmplName = mb_convert_encoding($get['import_tmpl_name'], 'GBK', 'UTF-8');//模版名称
        $importTmplAct  = $get['import_tmpl_act'];//导出模版方法

        if (!$importTmplAct) {
            //使用通用的导出模版
            $render->pagedata['tmpl_url'] = sprintf('index.php?app=omecsv&ctl=import&act=progress_import_tmplate&import_app=%s&&import_tmpl_name=%s&import_tmpl_mdl=%s', $importApp, $importTmplName, $importMdl);
        } else {
            //使用调用者的导出模版
            $render->pagedata['tmpl_url'] = sprintf('index.php?app=%s&ctl=%s&act=%s', $importApp, $importCtl, $importTmplAct);
        }

        $render->pagedata['import_app'] = $importApp;
        $render->pagedata['import_ctl'] = $importCtl;
        $render->pagedata['import_act'] = $importAct;
        $render->pagedata['import_mdl'] = $importMdl;
        $render->pagedata['import_filter'] = $importFilter;
        $render->pagedata['import_tmpl_name'] = $importTmplName;
        $render->pagedata['finder_id'] = $_GET['_finder']['finder_id'];

        $render->display('common/progress_import.html', 'omecsv');
    }

    /**
    * 进度条导入模版下载
    */
    public function progress_import_tmplate()
    {
        $get = kernel::single('base_component_request')->get_get();
        $importApp  = $get['import_app'];
        $importMdl  = $get['import_tmpl_mdl'];
        $importTmplName = $get['import_tmpl_name'] . date('Y-m-d') . '.csv';

        if (empty($importApp) || empty($importMdl)) {
            die('参数传递错误');
        }
        
        header("Content-Type: text/csv");
        header("Content-Disposition: attachment; filename={$tmplName}");
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');

        $mdl = app::get($importApp)->model($importMdl);

        if (method_exists($mdl, 'io_title')) {
            $ioTitle = $mdl->io_title('csv');

            foreach ($ioTitle as $v){
                $title[] = kernel::single('base_charset')->utf2local($v);
            }
            
            echo '"' . implode('","', $title) . '"';
            echo "\n\n";
        } else {
            die('mdl:' . $importMdl . 'not exists io_title method');
        }
    }

}
